package com.pcbly.web.components.email.sms;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.smslib.GatewayException;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;

import com.pcbly.web.bean.crm.SendMsgLogBean;

public class SMSEngine {
	private static Logger logger = Logger.getLogger(SMSEngine.class);
	private static SMSEngine engine = null; // 采用单例模式设置
	private Service service = null; // 短信的服务类

	private SMSEngine() {

	}

	public static SMSEngine getSMSEngine() {
		synchronized (SMSEngine.class) {
			if (engine == null) {
				engine = new SMSEngine();
			}
		}
		return engine;
	}

	/**
	 * 初始化服务
	 * 
	 */
	public void initService() throws Exception {
		service = new Service();// NEW 一个服务
		// NEW 一个GSM调制解调器 ↓(网关) ↓(端口) ↓(波特率)

		// windows下处理
		// SerialModemGateway gateway = new
		// SerialModemGateway("modem.com1","COM1", 9600, "Nokia", "6310i");

		// linux下处理
		SerialModemGateway gateway = new SerialModemGateway("SWSMS", "ttyS0",
				9600, "Nokia", "6310i");

		gateway.setInbound(true); // 是否可以接受短信
		gateway.setOutbound(true);// 是否可以发送短信
		gateway.setSimPin("0000");// 设置SIM卡的个人识别号码

		service.addGateway(gateway);// 添加一个GSM调制解调器

		try {
			service.startService(); // 开始服务
		} catch (Exception e) {
			logger.error("短信猫启动失败,失败原因:" + e.getMessage());
			service.stopService();
		}
	}

	/**
	 * 发送信息
	 * 
	 * @param receptMobiles
	 *            手机号组
	 * @param msg
	 *            消息体
	 */
	public List<SendMsgLogBean> sendMsg(String sendLinkman, String msg,
			String user_id) {

		List<SendMsgLogBean> sendMsgLogs = new ArrayList<SendMsgLogBean>(); // 日志列表
		List<HashMap<String, String>> sendList = new ArrayList<HashMap<String, String>>();// fsdxDao.getLinkmanById(sendLinkman);

		if (sendList != null && sendList.size() > 0) {

			for (int i = 0; i < sendList.size(); i++) {
				HashMap<String, String> map = sendList.get(i);
				OutboundMessage obmsg = new OutboundMessage(map.get("yddh")
						.toString(), msg);
				obmsg.setEncoding(MessageEncodings.ENCUCS2);// 设置字符集
				SendMsgLogBean sendMsgLog = new SendMsgLogBean();
				sendMsgLog.setUserName(map.get("name"));

				sendMsgLog.setMobileNum(map.get("yddh"));

				sendMsgLog.setCzDate(new Date());

				sendMsgLog.setCzr(user_id);

				sendMsgLog.setSendTime(new Date());

				try {
					if (service.sendMessage(obmsg) == true) {
						sendMsgLog.setFlag("1");

					} else {
						sendMsgLog.setFlag("0");

					}

				} catch (TimeoutException timeoutException) {
					sendMsgLog.setErrorLog("传送中超时");
				} catch (GatewayException gatewayException) {
					sendMsgLog.setErrorLog("网关连接错误");
				} catch (IOException ioException) {
					sendMsgLog.setErrorLog("传送错误");
				} catch (InterruptedException interruptedException) {
					sendMsgLog.setErrorLog("线程中断错误");
				}
				sendMsgLogs.add(sendMsgLog);
			}
			// fsdxDao.insertSendMsgLog(sendMsgLogs);
		}

		return sendMsgLogs;
	}
}
